<?php
// error_reporting(E_ALL & ~ E_NOTICE);
//echo file_put_contents("test.txt","Hello World. Testing!".time());

require_once 'mongo.php';

// Create the output object.

header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Credentials:true");
header("Access-Control-Allow-Headers:Content-Type,Accept,X-Requested-With");
header("Access-Control-Allow-Methods:GET,POST");

$callback = @$_REQUEST['callback'];
$table = @$_REQUEST['table'];


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	// $output =  array('success' => true);
	$postJson = file_get_contents('php://input');
	// echo $postJson;
	$output = insert($table , json_decode($postJson));

} elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
	// $output = array(array('score' => '100', 'askid' => 'Banana'),array('score' => '200', 'askid' => 'Banana'));
	$output = find($table);
}


//start output
if ($callback) {
    header('Content-Type: text/javascript');
    echo $callback . '(' . json_encode($output) . ');';
} else {
    header('Content-Type: application/x-json');
    echo json_encode($output);
}


// 'Access-Control-Allow-Origin' *;
// 'Access-Control-Allow-Credentials' 'true';
// 'Access-Control-Allow-Headers' 'Content-Type,Accept,X-Requested-With';
// 'Access-Control-Allow-Methods' 'GET, POST';
?>